using System;
using System.Collections.Generic;
using System.Text;
using System.Windows.Forms;
using System.Drawing;
using System.Diagnostics;

public class ProgressBarColumn : DataGridViewColumn
{
	public ProgressBarColumn()
		: base(new ProgressBarCell())
	{
	}

	public override DataGridViewCell CellTemplate
	{
		get
		{
			return base.CellTemplate;
		}
		set
		{
			// Ensure that the cell used for the template is a ProgressBarCell.
			if (value != null &&
				!value.GetType().IsAssignableFrom(typeof(ProgressBarCell)))
			{
				throw new InvalidCastException("Must be a ProgressBarCell");
			}
			base.CellTemplate = value;
		}
	}
}

public class ProgressBarCell : DataGridViewTextBoxCell
{

	public ProgressBarCell()
		: base()
	{
	}

	public override void InitializeEditingControl(int rowIndex, object initialFormattedValue, DataGridViewCellStyle dataGridViewCellStyle)
	{
		// Set the value of the editing control to the current cell value.
		base.InitializeEditingControl(rowIndex, initialFormattedValue, dataGridViewCellStyle);
		//ProgressEditingControl ctl = DataGridView.EditingControl as ProgressEditingControl;

		//ctl.Value = int.Parse(initialFormattedValue.ToString());
	}

	protected override void Paint(Graphics graphics, Rectangle clipBounds, Rectangle cellBounds, int rowIndex, DataGridViewElementStates cellState, object value, object formattedValue, string errorText, DataGridViewCellStyle cellStyle, DataGridViewAdvancedBorderStyle advancedBorderStyle, DataGridViewPaintParts paintParts)
	{
		Brush foreBrush = new SolidBrush(cellStyle.ForeColor);
		Brush backBrush = new SolidBrush(cellStyle.BackColor);
		Brush progressBrush = new SolidBrush(Color.SteelBlue);
		int centerText = cellBounds.X + cellBounds.Width / 2 - 3;
		try
		{
			//StringFormat sf = new StringFormat();
			
			decimal progressVal = decimal.Parse(value.ToString());
			decimal percentage = (decimal)progressVal / 100;
			if (this.DataGridView.CurrentRow.Index == rowIndex)
			{
				graphics.FillRectangle(backBrush, cellBounds);
			}
			else
			{
				graphics.FillRectangle(backBrush, cellBounds);
			}
			if (percentage > 0)
			{
				graphics.FillRectangle(progressBrush, cellBounds.X + 2, cellBounds.Y + 2, Convert.ToInt32((percentage * cellBounds.Width - 4)), cellBounds.Height - 4);
				graphics.DrawString(progressVal.ToString("#.##") + "%", cellStyle.Font, foreBrush, centerText, cellBounds.Y + 2);
			}
			else
			{
				if (this.DataGridView.CurrentRow.Index == rowIndex)
				{
					graphics.DrawString(progressVal.ToString("#.##") + "%", cellStyle.Font, progressBrush, centerText, cellBounds.Y + 2);
				}
				else
				{
					graphics.DrawString(progressVal.ToString("#.##") + "%", cellStyle.Font, foreBrush, centerText, cellBounds.Y + 2);
				}
			}
		}
		catch (Exception)
		{
			//Debug.WriteLine("DataGridViewProgressBarColumn::Paint:Error: " + ex.Message);
		}
	}


	public override Type EditType
	{
		get
		{
			// Return the type of the editing contol that ProgressBarCell uses.
			return typeof(ProgressEditingControl);
		}
	}

	public override Type ValueType
	{
		get
		{
			// Return the type of the value that ProgressBarCell contains.
			return typeof(Int32);
		}
	}

	public override object DefaultNewRowValue
	{
		get
		{
			// Use the current date and time as the default value.
			return 0;
		}
	}
}

class ProgressEditingControl : ProgressBar, IDataGridViewEditingControl
{
	DataGridView dataGridView;
	private bool valueChanged = false;
	int rowIndex;

	public ProgressEditingControl()
	{
		this.Minimum = 0;
		this.Maximum = 100;
	}

	// Implements the IDataGridViewEditingControl.EditingControlFormattedValue 
	// property.
	public object EditingControlFormattedValue
	{
		get
		{
			return this.Value.ToString();
		}
		set
		{
			if (value is string)
			{
				this.Value = int.Parse(value.ToString());
			}
		}
	}

	// Implements the 
	// IDataGridViewEditingControl.GetEditingControlFormattedValue method.
	public object GetEditingControlFormattedValue(
		DataGridViewDataErrorContexts context)
	{
		return EditingControlFormattedValue;
	}

	// Implements the 
	// IDataGridViewEditingControl.ApplyCellStyleToEditingControl method.
	public void ApplyCellStyleToEditingControl(
		DataGridViewCellStyle dataGridViewCellStyle)
	{
		this.Font = dataGridViewCellStyle.Font;
		this.ForeColor = dataGridViewCellStyle.ForeColor;
		this.BackColor = dataGridViewCellStyle.BackColor;
	}

	// Implements the IDataGridViewEditingControl.EditingControlRowIndex 
	// property.
	public int EditingControlRowIndex
	{
		get
		{
			return rowIndex;
		}
		set
		{
			rowIndex = value;
		}
	}

	// Implements the IDataGridViewEditingControl.EditingControlWantsInputKey 
	// method.
	public bool EditingControlWantsInputKey(
		Keys key, bool dataGridViewWantsInputKey)
	{
		// Let the DateTimePicker handle the keys listed.
		switch (key & Keys.KeyCode)
		{
			case Keys.Left:
			case Keys.Up:
			case Keys.Down:
			case Keys.Right:
			case Keys.Home:
			case Keys.End:
			case Keys.PageDown:
			case Keys.PageUp:
				return true;
			default:
				return false;
		}
	}

	// Implements the IDataGridViewEditingControl.PrepareEditingControlForEdit 
	// method.
	public void PrepareEditingControlForEdit(bool selectAll)
	{
		// No preparation needs to be done.
	}

	// Implements the IDataGridViewEditingControl
	// .RepositionEditingControlOnValueChange property.
	public bool RepositionEditingControlOnValueChange
	{
		get
		{
			return false;
		}
	}

	// Implements the IDataGridViewEditingControl
	// .EditingControlDataGridView property.
	public DataGridView EditingControlDataGridView
	{
		get
		{
			return dataGridView;
		}
		set
		{
			dataGridView = value;
		}
	}

	// Implements the IDataGridViewEditingControl
	// .EditingControlValueChanged property.
	public bool EditingControlValueChanged
	{
		get
		{
			return valueChanged;
		}
		set
		{
			valueChanged = value;
		}
	}

	// Implements the IDataGridViewEditingControl
	// .EditingPanelCursor property.
	public Cursor EditingPanelCursor
	{
		get
		{
			return base.Cursor;
		}
	}


	public Cursor EditingControlCursor
	{
		get { return base.Cursor; }

	}


}
